package portal;

import java.util.Locale;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import oracle.webcenter.navigationframework.ResourceNotFoundException;
import oracle.webcenter.portalframework.sitestructure.SiteStructure;
import oracle.webcenter.portalframework.sitestructure.SiteStructureContext;

import portal.bean.LocaleBean;

public class LanguagePhaseListener implements PhaseListener {
    
    private static final long serialVersionUID = 1L;

    public LanguagePhaseListener() {
        super();
    }

    public void afterPhase(PhaseEvent phaseEvent) {
    }

    public void beforePhase(PhaseEvent phaseEvent) {
        //FacesContext context = phaseEvent.getFacesContext();
        FacesContext context = FacesContext.getCurrentInstance(); 
        LocaleBean localeBean = (LocaleBean)context.getExternalContext().getSessionMap().get("localeBean");

        if(localeBean != null)
            context.getViewRoot().setLocale(localeBean.getUserLocale());
        else
            context.getViewRoot().setLocale(new Locale("vi","VN"));
        try {  
                      SiteStructureContext ctx = SiteStructureContext.getInstance(); 
                      SiteStructure model = ctx.getDefaultSiteStructure(); 
                      model.invalidateCache(); 
                    } catch (ResourceNotFoundException rnfe) { 
                        rnfe.printStackTrace(); 
                    } 

    }

    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }
}
